<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: ServiceProduct.php 5341 2010-09-07 09:04:47Z huuphuoc $
 * @since		2.0.5
 */

class Service_Models_Dao_Pdo_Mysql_ServiceProduct extends Tomato_Model_Dao
	implements Service_Models_Interface_ServiceProduct
{
	public function convert($entity) 
	{
		return new Service_Models_ServiceProduct($entity); 
	}
	public function find($offset,$count,$exp=null){
		$select = $this->_conn
					->select()
					->from(array('c' => $this->_prefix . 'service_product'))
					->joinInner(array('p' => $this->_prefix . 'product'), "c.product_id = p.id", array("product_name_vi_VN","product_name_en_US","volume_total","original_price","sale_price","product_unit_use","volume_one"));
		if ($exp) {
            if (isset($exp['id'])) {
				$select->where("c.id=?",$exp['id']);
			}
			if(isset($exp["service_id"])){
				$select->where("c.service_id=?",$exp['service_id']);	
			}
			if(isset($exp["product_id"])){
				$select->where("c.product_id=?",$exp['product_id']);	
			}
		}		
        $select->order('c.dose_used DESC');
        if(null != $count){
            $select->limit($count, $offset);
        }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);		
	}
	
	
	public 	function count($exp){
		$select = $this->_conn
                        ->select()
                        ->from(array('c' => $this->_prefix . 'service_product'), array('num_service_product' => 'COUNT(*)'));
            if (isset($exp['id'])) {
				$select->where("c.id=?",$exp['id']);
			}
			if(isset($exp["service_id"])){
				$select->where("c.service_id=?",$exp['service_id']);	
			}
			if(isset($exp["product_id"])){
				$select->where("c.product_id=?",$exp['product_id']);	
			}
                        
       return $select->query()->fetch()->num_service_product;	
	}	
	
	public function getById($id) 
	{
		$row = $this->_conn
					->select()
					->from(array('c' => $this->_prefix . 'service_product'))
					->where('c.id = ?', $id)
					->limit(1)
					->query()
					->fetch();
		return (null == $row) ? null : new Service_Models_ServiceProduct($row);
	}

	public function add($service_product) 
	{
			$data = array(
							"id"				=> $service_product->id,
							"service_id"		=> $service_product->service_id,
							"product_id"		=> $service_product->product_id,
							"dose_used"			=> $service_product->dose_used
						);			
			if (isset($service_product->id) && $service_product->id != null) {
				$data['id'] = $service_product->id;
				$this->_conn->insert($this->_prefix . 'service_product', $data);
				return $service_product->id;				
			} else {
				$this->_conn->insert($this->_prefix . 'service_product', $data);
				return $this->_conn->lastInsertId($this->_prefix . 'service_product');
			}
	}
	
	public function update($service_product) 
	{
		$this->_conn->update($this->_prefix . 'service_product', 
							array(
								"id"				=> $service_product->id,
								"service_id"		=> $service_product->service_id,
								"product_id"		=> $service_product->product_id,
								"dose_used"			=> $service_product->dose_used
							), 
							array(
								'id = ?' => $service_product->id,
							));
	}
	
	public function delete($service_product) 
	{
		$this->_conn->delete($this->_prefix . 'service_product', 
							array(
								'id = ?' => $service_product->id,
							));
	}	
	public function deleteWithServiceId($service_id){
		$this->_conn->delete($this->_prefix . 'service_product', 
							array(
								'service_id = ?' => $service_id,
							));
	}
}